#ifndef __DH_dh_net_H__
#define __DH_dh_net_H__

#ifdef __cplusplus
extern "C"
{
#endif

#include <stdio.h>
#include "array.h"
#include "msocket.h"
#include "dh.h"

#define DH_DEFAULT_IP NULL
#define DH_DEFAULT_PORT 6033
#define DH_DEFAULT_BACKLOG 8
#define DH_DEFAULT_MAXCLI 16
#define DH_NET_PROTOCOL_BUF_LEN 255

    enum
    {
        DH_NET_PROTOCOL_FLAG_END,      // 结束符
        DH_NET_PROTOCOL_FLAG_VAL_PACK, // val区域分包标识(只在头包存在)
        DH_NET_PROTOCOL_FLAG_VAL,      // val区域
        DH_NET_PROTOCOL_FLAG_KEY_PACK, // key区域分包标识(只在头包存在)
        DH_NET_PROTOCOL_FLAG_KEY,      // key区域
        DH_NET_PROTOCOL_FLAG_LRU,      // 定时
        DH_NET_PROTOCOL_FLAG_COMM,     // 命令区域
        DH_NET_PROTOCOL_FLAG_START,    // 头包
    };

    typedef struct dh_net_protocol_st
    {
        uint8_t flag;
        char buf[DH_NET_PROTOCOL_BUF_LEN];
    } dh_net_protocol_t;

    typedef struct dh_net_str_st
    {
        uint8_t flag;
        char *data;
        size_t len;
        struct dh_net_str_st *next;
    } dh_net_str_t;

    typedef struct dh_net_cli_socket_st
    {
        socket_t sock;
        dh_net_str_t *slist;
        dh_net_str_t *last; // 最后一个节点指针
    } dh_net_cli_socket_t;

    typedef struct dh_net_recv_event_st
    {
        socket_t *sock;   // 服务器句柄
        array_t *cli_arr; // 客户端数组 (dh_net_socket_t)
        size_t cli_idx;   // 当前客户端下标
    } dh_net_recv_event_t;

    typedef char (*dh_net_recv_cb_impl)(dh_net_recv_event_t *env, void *userdata);

    extern socket_t dh_net_init(int *errstep);
    extern void dh_net_deinit(socket_t sock);
    extern char dh_net_loop(socket_t sock, dh_net_recv_cb_impl cb, void *userdata);

#ifdef __cplusplus
}
#endif
#endif